package cn.nubia.admin.application.controller;

import cn.nubia.admin.application.bean.Channel;
import cn.nubia.admin.application.query.ChannelQuery;
import cn.nubia.admin.application.service.ChannelService;
import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.springmvc.Response;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;

/**
 * Created by justin on 15/7/11.
 */
@Controller
@RequestMapping(value = "/admin/channel/")
public class ChannelController {
    @Autowired
    ChannelService channelService;

    @RequestMapping(value = "add",method = RequestMethod.GET)
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        modelAndView.addObject("channel",new Channel());
        return modelAndView;
    }

    @RequestMapping(value = "edit",method = RequestMethod.GET)
    public ModelAndView edit(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        Channel channel = channelService.get(id);
        modelAndView.addObject("channel",channel);
        return modelAndView;
    }

    @RequestMapping(value = "view",method = RequestMethod.GET)
    public ModelAndView view(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        Channel channel = channelService.get(id);
        modelAndView.addObject("channel",channel);
        modelAndView.addObject("readonly", "readonly");
        return modelAndView;
    }

    @RequestMapping(value = "del",method = RequestMethod.GET)
    public ModelAndView del(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        Channel channel = channelService.changeState(id, Channel.STATE_DEL);
        modelAndView.addObject("channel",channel);
        return modelAndView;
    }


    @RequestMapping(value = "show",method = RequestMethod.GET)
    public ModelAndView show(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        Channel channel = channelService.changeState(id, Channel.STATE_SHOW);
        modelAndView.addObject("channel",channel);
        return modelAndView;
    }


    @RequestMapping(value = "hidden",method = RequestMethod.GET)
    public ModelAndView hidden(Integer id){
        ModelAndView modelAndView = new ModelAndView("/client/channelForm");
        Channel channel = channelService.changeState(id, Channel.STATE_HIDDEN);
        modelAndView.addObject("channel",channel);
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Response<Boolean> save(@Valid Channel channel, BindingResult result){
        if(result.hasErrors())return Response.getFailedResponse(result.getAllErrors().get(0).getDefaultMessage());
        channelService.processSaveChannel(channel);
        return  Response.getSuccessResponse(true);
    }

    @RequiresPermissions(value = "soft:client:channelList")
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(ChannelQuery query){
        ModelAndView modelAndView = new ModelAndView("/client/channelList");
        Paged<Channel> channels = channelService.listChannels(query);
        modelAndView.addObject("channels", channels);
        modelAndView.addObject("query",query);
        return modelAndView;
    }
}
